const common = {
    mysleep(time) {
        return new Promise((resolve) => setTimeout(resolve, time));
    },

    checkType(any) {
        return Object.prototype.toString.call(any).slice(8, -1)
    },

    clone(any) {
        let retType = this.checkType(any);
        if (retType === 'Object') { // 拷贝对象
            let o = {};
            for (let key in any) {
                o[key] = this.clone(any[key])
            }
            return o;
        } else if (retType === 'Array') { // 拷贝数组
            var arr = []
            for (let i = 0, leng = any.length; i < leng; i++) {
                arr[i] = this.clone(any[i])
            }
            return arr;
        } else if (retType === 'Function') { // 拷贝函数
            return new Function('return ' + any.toString()).call(this)
        } else if (retType === 'Date') { // 拷贝日期
            return new Date(any.valueOf())
        } else if (retType === 'RegExp') { // 拷贝正则
            return new RegExp(any)
        } else if (retType === 'Map') { // 拷贝Map 集合
            let m = new Map()
            any.forEach((v, k) => {
                m.set(k, this.clone(v))
            })
            return m
        } else if (retType === 'Set') { // 拷贝Set 集合
            let s = new Set()
            for (let val of any.values()) {
                s.add(this.clone(val))
            }
            return s
        }
        return any;
    },
}

export default common;
